package br.edu.fa7.receita.persistencia;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import br.edu.fa7.receita.model.Comentario;

public class ComentarioDAO {
	
  private SQLiteDatabase db;
  private SQLiteHelper dbHelper;
  
//  private static final String SCRIPT_DB_DELETE = 
//    "DROP TABLE IF EXISTS comentarios";
//  
//  private static final String SCRIPT_DB_CREATE =
//    "create table comentarios (_id integer primary key autoincrement, " +
//    "descricao text not null, " +
//    "nota int not null, " +
//    "FOREIGN KEY (usuarioId) REFERENCES usuarios (_id), " +
//    "FOREIGN KEY (receitaId) REFERENCES receitas (_id) );";

  public ComentarioDAO (Context ctx){
    dbHelper = new SQLiteHelper(ctx, "comentarios", 1);
  }

  @SuppressWarnings("unused")
  private long inserir(Comentario comentario){
    ContentValues cv = new ContentValues();
    cv.put("descricao", comentario.getDescricao());
    cv.put("nota", comentario.getNota());
    cv.put("usuarioId", comentario.getUsuarioId());
    cv.put("receitaId", comentario.getReceitaid());
    
    db = dbHelper.getWritableDatabase();
    long id = db.insert("comentarios", null, cv);
    db.close();
    return id;
  }

  @SuppressWarnings("unused")
  private long atualizar(Comentario comentario){
    ContentValues cv = new ContentValues();
    cv.put("descricao", comentario.getDescricao());
    cv.put("nota", comentario.getNota());
    cv.put("usuarioId", comentario.getUsuarioId());
    cv.put("receitaId", comentario.getReceitaid());
    
    db = dbHelper.getWritableDatabase();
    long rows = db.update("comentarios", cv, "_id = ?", 
       new String[]{ String.valueOf(comentario.getId())});
    db.close();
    return rows; // qtde. de linhas afetadas 
  } 

  public int excluir(int id){
    db = dbHelper.getWritableDatabase();
    int rows = db.delete("comentarios", "_id = ?", 
      new String[]{ String.valueOf(id) });
    return rows; // qtde. de linhas afetadas
  }

  public List<Comentario> buscarComentarioPorReceita(int idReceita){
    List<Comentario> lista = new ArrayList<Comentario>();

    String[] columns = new String[]{"_id", "descricao", "nota", "usuarioId", "receitaId"};
    
    String[] args = new String[]{String.valueOf(idReceita)};

    db = dbHelper.getWritableDatabase();
    Cursor c = db.query("comentarios", columns, 
       "receitaId = ", args, null, null, "_id DESC");

    c.moveToFirst();
    
    while(!c.isAfterLast()){
    	Comentario comentario = fillComentario(c);
    	lista.add(comentario);
    	c.moveToNext();
    }
    
    c.close();
    db.close();
    return lista;
  }

  private Comentario fillComentario(Cursor cursor) {
    Comentario comentario = new Comentario();
    comentario.setId((int)cursor.getLong(0));
    comentario.setDescricao(cursor.getString(1));
    comentario.setNota(cursor.getInt(2));
    comentario.setUsuarioId(cursor.getInt(3));
    comentario.setReceitaid(cursor.getInt(4));
    return comentario;
  }
}